package com.hongyuan.sellcoach.ui.member.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.hongyuan.sellcoach.R;
import com.hongyuan.sellcoach.base.AppViewModelFactory;
import com.hongyuan.sellcoach.base.CustomFragment;
import com.hongyuan.sellcoach.data.Constants;
import com.hongyuan.sellcoach.databinding.FragmentMemberBinding;
import com.hongyuan.sellcoach.ui.member.view_mode.MemberViewModel;
import com.hongyuan.sellcoach.utils.CustomDialog;

import me.tatarka.bindingcollectionadapter2.BR;

public class MembersFragment extends CustomFragment<FragmentMemberBinding, MemberViewModel> {


    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_member;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MemberViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MemberViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();
        setOnRefresh(binding.refresh,REFRESH_0X3);
        setEnableLoadMore(Constants.GET_COACH_MEMBERS);
        setEnableRefresh(Constants.GET_COACH_MEMBERS);
    }

    @Override
    public void initData() {
        super.initData();

        viewModel.requestData(Constants.OS_LIST);

        viewModel.setParams("type","1")
                .requestData(Constants.GET_COACH_MEMBERS);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.showOs.observe(this,list -> {
            CustomDialog.scroller(getContext(), list, "选择门店", (v, message,id) -> {
                viewModel.osName.set(message);
                binding.osName.setTag(id);
                if("全部门店".equals(message)){
                    id = "";
                    viewModel.osId.set("");
                }else{
                    viewModel.osId.set(id);
                }
                viewModel.setParams("os_id",id).requestData(Constants.GET_COACH_MEMBERS);
            });
        });
    }
}
